 /*
 * UMLSynch - Simple RMI part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.rpc.base;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class TOptions {
	private	TConfig[] configs;
	private TDebug debug;
	private static String defaul_configfile = "d:/eclipse_vpsde_rpc.cfg";

	public TOptions(IDebug dbg) {
		debug = new TDebug(dbg);
		this.ParseConfigfile(defaul_configfile);
	}
	
	public void ParseConfigfile(String configfile) {
		try {
			FileReader fr;
			fr = new FileReader(configfile);
			BufferedReader br = new BufferedReader(fr);
			int cc = 0;
			
			while (br.ready()) {
			   String sline = br.readLine();
			   String[] option = sline.split(":");
			   if (option[0] == "Service") {
				 // Start to Parse Next Config
				 if ((option[1] == "EclipseIndexer") 
					 || (option[1] == "VPSDEEDITOR")) {
					 if (configs[cc] != null) {
						 ++cc;
					 }
				 } else {
					 debug.Error("Unknown config option value:" + sline);
					 break;
				 }
				 configs[cc] = new TConfig();
			   } else if (option[0] == "Host") {
				   if (configs[cc] != null) {
					   configs[cc].hostname = option[1];
				   }
				   
			   } else if (option[0] == "Port") {
				   if (configs[cc] != null) {
					   configs[cc].port = Integer.parseInt(option[1]);
				   }
				   
			   } else if (option[0] == "Protocol") {
				   if (configs[cc] != null) {
					   configs[cc].protocol = option[1];
				   }
			   } else {
				   debug.Error("Unknown config option :" + option[0]);
				   break;
			   }
			}
		} catch (FileNotFoundException e) {
			debug.Error("Config file not found: " + configfile);
		} catch (IOException e) {
			debug.Error("Config file reading error: " + configfile);
		}
	}

	public
	TConfig getConfig(String service) {
		for (int i=0; i<configs.length; ++i) {
			if (configs[i].service == service) {
				return configs[i];
			}
		}
		debug.Error("Wrong service name"+ service);
		return null;
	}
}
